'''
Created on Sep 10, 2011

@author: TommySprat
'''
import pygame

class AnimationSystem:
    '''Sets up the variables needed for the animation system
    init_animsystem(args here'''
    def init(self, master):
        master.sprite = pygame.Surface((2, 2))
        master.spriteList = 0
        master.frame = 0
        master.frameClock = 0 #counter that limits the frame speed of the sprite
    
    def framestep(self, master, imagespeed, loop):
        '''framestep(master, imagespeed, loop) loop True or False. If False it returns 0 on animation end
        imagespeed is how many cycles a frame is shown.'''
        
        master.frameClock += 1
        if(master.frameClock == round(imagespeed)): #Divide the FPS by amount of frames for 1sec/loop
            master.frameClock = 0
            master.frame +=1
            if(master.frame > len(master.spriteList)-1):
                if (loop == 1):
                    master.frame = 0
                else:
                    return 0                
                
        try:
            master.sprite = master.spriteList[master.frame]
        except IndexError:
            master.frame = 0 #prevent sprite index from going out of range
            
def load_images(imageList, alpha=1):
    '''Takes a list of images and converts them. Returns a list of surfaces(sprites).'''
    processed_list = []
    if alpha:
        for image in imageList:
            processed_list.append(pygame.image.load(image).convert_alpha())
        return processed_list
    
    for image in imageList:
        processed_list.append(pygame.image.load(image))
    return processed_list